﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.Properties;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Defines a new node type that represents a custom action on a SharePoint site.
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeProvider))]
    // Specifies the ID for this new node type.
    [ExplorerNodeType(HideCustomActionNodeTypeProvider.HideCustomActionNodeTypeId)]
    class HideCustomActionNodeTypeProvider : IExplorerNodeTypeProvider
    {
        #region Constants

        /// <summary>
        /// The node type id
        /// </summary>
        internal const string HideCustomActionNodeTypeId = "SPVSX.SharePointHideCustomAction";

        #endregion

        #region Methods

        /// <summary>
        /// Initialise the node with its icon and event hookups
        /// </summary>
        /// <param name="typeDefinition">The node type defintion</param>
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon = Resources.HideCustomAction.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;

            typeDefinition.NodePropertiesRequested += NodePropertiesRequested;
        }

        /// <summary>
        /// Retrieves properties that are displayed in the Properties window when
        /// a hide custom action node is selected.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodePropertiesRequestedEventArgs object</param>
        private void NodePropertiesRequested(object sender,
            ExplorerNodePropertiesRequestedEventArgs e)
        {
            var hideCustomActionNodeInfo = e.Node.GetAnnotation<HideCustomActionNodeInfo>();

            // Call the custom SharePoint command to get the HideCustomAction properties.
            Dictionary<string, string> properties =
                e.Node.Context.SharePointConnection.ExecuteCommand<
                HideCustomActionNodeInfo, Dictionary<string, string>>(
                HideCustomActionCommandIds.GetHideCustomActionProperties, hideCustomActionNodeInfo);

            object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
            e.PropertySources.Add(propertySource);
        }

        #endregion
    }
}